import tools from '../../../utils/tools'

import Handle from './Handle'

/**
 * 负责列表按钮事件处理
 */
@tools
export default class Event {
  constructor(oList, courseData, cartData, observer) {
    this.oList = oList
    this.oAddBtns = this.oList.getElementsByClassName('add-btn')
    this.courseData = courseData
    this.cartData = cartData
    this.observer = observer

    this.init()
  }
  init() {
    this.bindEvent()

    this.handle = new Handle(
      this.courseData,
      this.cartData,
      this.oAddBtns,
      this.observer
    )
  }
  /**
   * 绑定事件
   */
  bindEvent() {
    // 添加购物车按钮
    this.oList.addEventListener('click', this.onAddBtnClick.bind(this), false)
  }
  onAddBtnClick(e) {
    // 获取当前点击的按钮
    const tar = Event.getTarget(e),
      className = tar.className
    if (className === 'add-btn') {
      const id = tar.getAttribute('data-id')
      this.observer.notify(this, id, tar, 1)
    }
  }
}
